package com.alan.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 打jar跑 hadoop jar mr.jar com.alan.mr.JobRun
 * Created by Alan on 2017/10/2.
 */
public class RunJob {
    public static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    static class HotMapper extends Mapper<LongWritable,Text,KeyPair,Text>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] ss = line.split("\t");
            if(ss.length == 2){
                try {
                    Date date = SDF.parse(ss[0]);
                    Calendar c = Calendar.getInstance();
                    c.setTime(date);
                    int year = c.get(1);
                    String hot = ss[1].substring(0,ss[1].indexOf("℃"));
                    KeyPair keyPair = new KeyPair();
                    keyPair.setYear(year);
                    keyPair.setHot(Integer.parseInt(hot));
                    context.write(keyPair,value);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class HotReduce extends Reducer<KeyPair,Text,KeyPair,Text>{
        @Override
        protected void reduce(KeyPair key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            for (Text v : values) {
                context.write(key,v);
            }
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        // 创建job对象
        Job job = Job.getInstance(conf);
        // 指定程序的入口
        job.setJobName("hot");
        job.setJarByClass(RunJob.class);
        job.setMapperClass(HotMapper.class);
        job.setReducerClass(HotReduce.class);
        job.setMapOutputKeyClass(KeyPair.class);
        job.setMapOutputValueClass(Text.class);

        job.setNumReduceTasks(3);//设置reduce任务的个数，默认为1个
        job.setPartitionerClass(FirstPartition.class);//分区
        job.setSortComparatorClass(SortHot.class);//排序
        job.setGroupingComparatorClass(GroupHot.class);//分组

        //mapreduce 输入数据所丰目录或者文件 HDFS
        FileInputFormat.setInputPaths(job, new Path("/tmp/input/hot/"));
        //mr执行后的输出数据的目录
        FileOutputFormat.setOutputPath(job, new Path("/tmp/output/hot"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }

}
